package day1201parking;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class CarTest {
	ArrayList<CarSpot> list = new ArrayList<>();// 存储各个车位的信息
	ArrayList<Car> list1 = new ArrayList<>();// 存储停车位中车的信息
	ArrayList<String> billlist = new ArrayList<>();
	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		new CarTest().start();
	}

	// 主页面
	public void start() {
		while (true) {
			System.out.println("---请选择功能----");
			System.out.println("----1-增加车位--");
			System.out.println("----2-停车--");
			System.out.println("----3-查询停车位的状态--");
			System.out.println("----4-取车--");
			System.out.println("----5-查询全部收费记录--");
			System.out.println("----6-退出系统--");
			switch (Integer.parseInt(sc.nextLine())) {
			case 1:
				spotAdd();
				break;
			case 2:
				setCar();
				break;
			case 3:
				findStatus();
				break;
			case 4:
				carBack();
				break;
			case 5:
				findAllBill();

				break;
			case 6:

				return ;
			default:
				System.out.println("输入指令有误!!!");
			}
		}

	}
	//查询所有账单信息
	public void findAllBill() {
		ArrayList<BillRecordModel> list = new BillRecordDao().select();
		if(list.isEmpty()) {
			System.out.println("暂时没有收费记录");
		}else {
			for(BillRecordModel br : list)
				System.out.println(br.toString());
		}
	}

	//取车
	public void carBack() {
		System.out.println("请输入您停的车位号：");
		String id = sc.nextLine();
	    CarSpot cs = new CarSpot();
	    cs.setId(id);cs.setStatus("off");
	    ArrayList<CarSpot> li = new CarSpotDao().select(cs);
	    if(li.isEmpty()) {
	    	System.out.println("当前车位没有停车，输入有误");
	    }else {
	    	System.out.println("请输入您的车牌号:");
	    	String number = sc.nextLine();
	    	if(li.get(0).getNumber().equals(number)) {
	    		System.out.println("取车成功！！！");
	    		CarSpot spot = new CarSpot();
	    		spot.setId(id);
	    		spot.setStatus("off");
	    		new CarSpotDao().update(spot);
	    		ArrayList<Car> list = new CarDao().select(number);
	    		Car car = list.get(0);
	    		long etime = getTime();
	    		car.setEtime(etime);
	    		BillRecordModel bill = new BillRecordModel();
	    		bill.setId(id);bill.setSalary(car.getBill());
	    		new BillRecordDao().insert(bill);
	    		new CarDao().delete(number);
	    	}else {
	    		System.out.println("输入信息有误！");
	    	}
	    }
	}

	//输出目前全部车位的状态
	public void findStatus() {
		CarSpot cs = new CarSpot();
		ArrayList<CarSpot> list = new CarSpotDao().select(cs);
		if(list.isEmpty()) {
			System.out.println("目前没有车位信息");
		}else {
			for(CarSpot s : list)
				System.out.println(s.toString());
		}
		
	}

	
	//停车
	public void setCar() {
		CarSpot c = new CarSpot();c.setStatus("on");
		System.out.println("目前可以停车的车位有：");
		ArrayList<CarSpot> list = new CarSpotDao().select(c);
		if(list.isEmpty()) {
			System.out.println("目前没有空余车位");
			return ;
		}else {
			for(CarSpot s : list)
				System.out.println(s.toString());
		}
		System.out.println("请输入您想停的车位号：");
		String id = sc.nextLine();
		System.out.println("请输入您的车高度：");
		double height = sc.nextDouble();sc.nextLine();
		if(height>3.0) {
			System.out.println("不符合车高要求，不允许停车");
			return ;
		}
		System.out.println("请输入车牌号:");
		String number = sc.nextLine();
		if(number==null||number.trim().equals("")) {
			System.out.println("没有车牌号，不允许停车");
			return ;
		}
		Car car = new Car();
		car.setHeight(height);
		car.setId(id);
		car.setNumber(number);
		car.setStime(getTime());
		new CarDao().insert(car);
		CarSpot cs = new CarSpot();
		cs.setId(id);
		cs.setStatus("on");
		cs.setNumber(number);
		int res = new CarSpotDao().update(cs);
		if(res!=0) {
			System.out.println("停车成功");
		}else {
			System.out.println("停车失败");
			return ;
		}
	}

	
	//获取当前时间毫秒数
	public long getTime() {
		Date date = new Date();
		long str = date.getTime();
		return str;
	}

	//用来增加停车位
	public void spotAdd() {
		while (true) {
			input();
			System.out.println("是否继续录入？y/n ");
			if (!"y".equalsIgnoreCase(sc.nextLine()))
				break;
		}
	}

	public void input() {
		System.out.println("请输入要添加的车位号：");
		String id = new String();
		do {
			id = sc.nextLine();
		} while (isExist(id));
		CarSpot c = new CarSpot();
		c.setId(id);
		c.setStatus("on");
		c.setNumber(null);
		new CarSpotDao().insert(c);
		System.out.println("添加成功!");
	}

	//判断目前增加的车位是否已经被增加
	public boolean isExist(String id) {
		CarSpot cs = new CarSpot();cs.setId(id);
		ArrayList<CarSpot> list = new CarSpotDao().select(cs);
		if(list.isEmpty()) {
			return false;
		}else {
			System.out.println("该车位号已经存在");
			return true;
		}
	}

}
